/*
 * ColumnSelectionDialog.java
 */

package org.owasp.scrubbr.db.report;

import org.owasp.scrubbr.db.Table;
import java.awt.Dimension;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableColumn;


public class ColumnSelectionDialog extends JDialog {
    
    private Table table;
    private javax.swing.JButton btnOK;
    private javax.swing.JCheckBox[] jCheckBox;
    
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JButton showAllButton;
    
    private JTable jTable;
    
    private static final int maxColumns = 999;
    
    /** Creates a new instance of ColumnSelectionDialog */
    public ColumnSelectionDialog() {
        initComponents();
    }
    
    public ColumnSelectionDialog(JTable jt) {
        jTable = jt;
        initComponents();
    }
    
    public int getMaxColumns(){
        return maxColumns;
    }
    
    public boolean isColumnVisible(int i){
        return jCheckBox[i].isSelected();
    }
    
    public void setTable(Table t) {
        
        table = t;
        int i = 0;
       
        setTitle("Pick Display Columns");
        setResizable(false);
        setSize( 300, 300 );
        setPreferredSize( new Dimension((t.getAllColumns().size()/15)*150, 300) );
        for(; i < t.getAllColumns().size() && i < maxColumns; i++) {
            jCheckBox[i].setText(t.getAllColumns().get(i));
            jCheckBox[i].setVisible(true);
            jCheckBox[i].setSelected(true);
        }
        
        for(;i<maxColumns;i++) {
            jCheckBox[i].setText("");
            jCheckBox[i].setVisible(false);
        }
        
    }
 
    private void initComponents() {
        okButton = new javax.swing.JButton();
        showAllButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        
        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });
        
        
        showAllButton.setText("Show All");
        showAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllActionPerformed(evt);
            }
        });
        
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        
        jCheckBox = new JCheckBox[maxColumns];
        
        for (int i = 0;i<maxColumns;i++){
        
            jCheckBox[i] = new javax.swing.JCheckBox();
        }
        
        for(int i=0;i<maxColumns;i++) {
            jCheckBox[i].setSelected(true);
            jCheckBox[i].setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
            jCheckBox[i].setMargin(new java.awt.Insets(0, 0, 0, 0));
            jCheckBox[i].setText("");
        }
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);       

        ParallelGroup cbParallelGroup = jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING);        
       
        for(int i=0;i<maxColumns;i++) {
            cbParallelGroup.addComponent(jCheckBox[i]);
        }
        
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(cbParallelGroup)
                .addContainerGap(176, Short.MAX_VALUE))
        );
        
        SequentialGroup cbSequentialGroup = jPanel1Layout.createSequentialGroup();
        
        cbSequentialGroup.addContainerGap();
        
        for(int i=0;i<maxColumns;i++) {
            
            //if first, don't include the prefered gap
            cbSequentialGroup.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
            cbSequentialGroup.addComponent(jCheckBox[i]);
        }
        
        cbSequentialGroup .addContainerGap(116, Short.MAX_VALUE);
        
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup( cbSequentialGroup )
                );
        
        jScrollPane1.setViewportView(jPanel1);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(showAllButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton, showAllButton});

        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cancelButton)
                .addComponent(okButton)
                .addComponent(showAllButton))
                .addContainerGap())
                );
        pack();
    }
    
    
    private void btnShowAllActionPerformed(java.awt.event.ActionEvent evt) {
        
        //check all the boxes, then call "OK"
        for (int i = 0;i<this.getMaxColumns();i++){
            jCheckBox[i].setSelected(true);
        }
        
        btnOKActionPerformed(evt);
    }
    
    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
        setVisible(false);
        /* update UI to only show some columns */
        
           XTableColumnModel columnModel = (XTableColumnModel)jTable.getColumnModel();
            
            for (int i = 0;i<this.getMaxColumns();i++){
                
                //System.out.println("column " + i + " visibility = " + this.isColumnVisible(i));
                TableColumn column  = columnModel.getColumnByModelIndex(i);
                columnModel.setColumnVisible(column, this.isColumnVisible(i));
        }
        
        jTable.tableChanged(new TableModelEvent(jTable.getModel()));
    }
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        setVisible(false);
    }
        
    private void closeDialog(java.awt.event.WindowEvent evt) {
        setVisible(false);
    }
    
}
